﻿namespace TuneMultiCheckBox.Utils
{
  using System;
  using System.Linq.Expressions;

  public static class PropertyHelper
  {
    /// <summary>
    /// Extracts the name of the property.
    /// </summary>
    /// <typeparam name="T">Property Type</typeparam>
    /// <param name="propertyExpression">The property expression.</param>
    /// <returns>
    /// Property Name
    /// </returns>
    /// <exception cref="System.ArgumentException">@Expression must be a MemberExpression.;propertyExpression</exception>
    public static string ExtractPropertyName<T>(this Expression<Func<T>> propertyExpression)
    {
      var memberExpression = propertyExpression.Body as MemberExpression;

      if (memberExpression == null)
      {
        throw new ArgumentException(@"Expression must be a MemberExpression.", "propertyExpression");
      }

      return memberExpression.Member.Name;
    }
  }
}
